package cn.edu.neu.softlab633.influencemaximization.sy.Index;

import javax.sound.sampled.Line;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jason on 2017/5/7.
 */
public class NetworkIndex {

    public static Map getReverseIndex(String filepath) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(filepath));
        Map<Long, ArrayList<Long>> index = new HashMap<>();
        String line = br.readLine();
        line = br.readLine();
        while (line != null) {
            long id1 = Long.valueOf(line.split(",")[0]);
            long id2 = Long.valueOf(line.split(",")[1]);
            if (index.containsKey(id2)) {
                index.get(id2).add(id1);
            } else {
                ArrayList<Long> tmp = new ArrayList<>();
                tmp.add(id1);
                index.put(id2, tmp);
            }
            line = br.readLine();
        }
        br.close();
        return index;
    }

    public static Map getIndex(String filepath) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(filepath));
        Map<Integer, ArrayList<Integer>> index = new HashMap<>();
        String line = br.readLine();
        line = br.readLine();
        while (line != null) {
            int id1 = Integer.valueOf(line.split(",")[0]);
            int id2 = Integer.valueOf(line.split(",")[1]);
            if (index.containsKey(id1)) {
                index.get(id1).add(id2);
            } else {
                ArrayList<Integer> tmp = new ArrayList<>();
                tmp.add(id2);
                index.put(id1, tmp);
            }
            line = br.readLine();
        }
        br.close();
        return index;
    }

    public static void main(String[] args) throws IOException {
        getReverseIndex("data/test.txt");
    }
}
